/*
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package gui;

import interfaces.EditPanel;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.Serializable;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

/**
 * The container panel for settings
 * 
 * 
 */
public class DialogPanel extends JPanel {
   public static class TwoStringComparatorIgnoreCase implements Comparator<String>, Serializable {
	  private static final long serialVersionUID = 7810262104210474620L;

	  @Override
	  public int compare(final String object1, final String object2) {
		 return object1.compareToIgnoreCase(object2);
	  }

   }

   private static final long serialVersionUID = 6110973105032742699L;
   private JPanel toolBar = null;
   private JComboBox itemListBox = null;
   private JPanel itemDisplay = null;
   private List<String> itemList = null;
   private EditPanel editPanel = null;

   private JButton saveItemButton;

   public DialogPanel() {
	  super();
   }

   /**
    * Deletes the current item
    */
   private void deleteItem() {
	  if (editPanel.delete()) {
		 final String selected = (String) itemListBox.getSelectedItem();
		 removeFromList(selected);
	  } else {
		 JOptionPane.showMessageDialog(this, "Failed to delete the item", "Error", JOptionPane.ERROR_MESSAGE);
	  }
   }

   /**
    * Initiates the panel
    * 
    * @param itemList
    * @param editPanel
    * @param enableNewAndDelete
    * @return
    */
   public DialogPanel init(final List<String> itemList, final EditPanel editPanel, final boolean enableNewAndDelete) {
	  this.itemList = itemList;
	  this.editPanel = editPanel;
	  setLayout(new BorderLayout());

	  toolBar = new JPanel();
	  toolBar.setLayout(new FlowLayout());

	  final JButton newItemButton = new JButton("New");
	  newItemButton.addActionListener(new ActionListener() {
		 @Override
		 public void actionPerformed(final ActionEvent e) {
			final String s = (String) JOptionPane.showInputDialog(toolBar, "Name of the new item:", "New Item Name",
				  JOptionPane.PLAIN_MESSAGE, null, null, "");
			if (!"".equals(s)) {
			   newItem(s);
			}
		 }
	  });
	  newItemButton.setEnabled(enableNewAndDelete);

	  saveItemButton = new JButton("Save");
	  saveItemButton.addActionListener(new ActionListener() {
		 @Override
		 public void actionPerformed(final ActionEvent e) {
			saveItem();
		 }
	  });

	  final JButton deleteItemButton = new JButton("Delete");
	  deleteItemButton.addActionListener(new ActionListener() {
		 @Override
		 public void actionPerformed(final ActionEvent e) {
			deleteItem();
		 }
	  });
	  deleteItemButton.setEnabled(enableNewAndDelete);

	  itemListBox = new JComboBox();
	  itemListBox.addActionListener(new ActionListener() {
		 @Override
		 public void actionPerformed(final ActionEvent e) {
			final JComboBox cb = (JComboBox) e.getSource();
			if (cb.getSelectedIndex() < 0)
			   return;
			final String itemName = (String) cb.getSelectedItem();
			updateItemDisplay(itemName);
		 }
	  });

	  toolBar.add(newItemButton);
	  toolBar.add(saveItemButton);
	  toolBar.add(deleteItemButton);
	  toolBar.add(itemListBox);

	  itemDisplay = new JPanel();
	  itemDisplay.setLayout(new FlowLayout());

	  loadItemListBox();
	  add(toolBar, BorderLayout.NORTH);
	  add(itemDisplay, BorderLayout.CENTER);
	  setVisible(true);
	  validate();
	  saveItemButton.setEnabled(false);
	  return this;
   }

   /**
    * Loads the list of items into the itemlistbox
    */
   private void loadItemListBox() {
	  itemListBox.removeAllItems();
	  itemListBox.validate();
	  for (final String s : itemList) {
		 itemListBox.addItem(s);
	  }
	  itemListBox.validate();
   }

   /**
    * Creates a new item
    * 
    * @param itemName
    */
   private void newItem(final String itemName) {
	  if (itemName == null || itemName.length() < 1 || itemList.contains(itemName))
		 return;

	  itemList.add(itemName);
	  Collections.sort(itemList, new TwoStringComparatorIgnoreCase());
	  loadItemListBox();
	  itemListBox.setSelectedItem(itemName);
   }

   /**
    * Removes an item from the item list and item list box
    * 
    * @param item
    */
   public void removeFromList(final String item) {
	  itemDisplay.removeAll();
	  itemDisplay.validate();
	  itemListBox.removeItem(item);
	  itemList.remove(item);
   }

   /**
    * Saves the current item
    */
   private void saveItem() {
	  if (editPanel.save()) {
		 final String selected = (String) itemListBox.getSelectedItem();
		 if (!selected.equals(editPanel.getItemName())) {
			itemListBox.removeItem(selected);
			itemList.add(editPanel.getItemName());
			Collections.sort(itemList);
			loadItemListBox();
			itemListBox.setSelectedItem(editPanel.getItemName());
		 }
		 setSaveButtonEnabled(false);
	  } else {
		 JOptionPane.showMessageDialog(this, "Failed to save the item", "Error", JOptionPane.ERROR_MESSAGE);
	  }
   }

   /**
    * Sets that enabled status of the save button
    * 
    * @param in
    */
   public void setSaveButtonEnabled(final boolean in) {
	  saveItemButton.setEnabled(in);
   }

   /**
    * Loads the selected item into the display
    * 
    * @param itemName
    */
   private void updateItemDisplay(final String itemName) {
	  itemDisplay.removeAll();
	  itemDisplay.validate();
	  itemDisplay.add(editPanel.load(itemName));
	  itemDisplay.validate();
	  saveItemButton.setEnabled(false);
   }
}
